(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:pager
     * @description
     * # pager 
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.aurl.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('aUrl', function() {
            return {
                restrict: "E",
                template: " <div ng-if='iconbtn'>链接 <div  class='input-control control-right' ng-click='showPage();'>" +
                    "<input type='text' class='input-fat' ng-model='pagepath' disabled='true' style='width:170px;padding-right:20px;'><i class='sui-icon icon-tb-link'></i>" +
                    "</div></div><div ng-if='!iconbtn' style='display:inline-block;'><a href='javascript:void(0);' class='sui-btn btn-small btn-warning' ng-click='showPage();'>添加链接</a> <a href='javascript:void(0);' class='sui-btn btn-small btn-danger' ng-show='!rm' ng-click='remove();'>移除链接</a>",
                scope: {
                    setback: "&",
                    seclectdata: "=",
                    pagepath: "=",
                    rm: "&",
                    iconbtn: "=",
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout) {
                    var showDialog = null
                    $scope.cid = "";
                    var cpath = "";
                    var mapObj = null;
                    $scope.remove = function() {
                        $timeout(function() {
                            if ($scope.setback) {
                                $scope.setback("");
                                return true;
                            }
                        });
                    }
                    $scope.showPage = function() {
                        $scope.aurl = {
                            ctype: "",
                            phone: ''
                        };
                        localtion = null;
                        address = null;
                        if ($scope.seclectdata) {
                            $scope.aurl = $scope.seclectdata;
                            // console.error("$scope.seclectdata", $scope.seclectdata);
                            if ($scope.seclectdata.ctype == "phone") {
                                $scope.aurl.phone = $scope.seclectdata.click_path;
                            }
                        }
                        // console.error("$scope.aurl", $scope.path, $scope.seclectdata);
                        $scope.path = "";
                        if ($scope.pagepath) {
                            $scope.path = $scope.pagepath;
                        }
                        if ($scope.aurl && $scope.aurl.click_path) {
                            $scope.path = $scope.aurl.click_path;
                        }
                        showDialog = $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '<span>+选择链接类型</span>',
                            body: $templateCache.get("views/directives.aurl.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            // hasfoot: false,
                            width: '880px',
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $scope.setdata();
                            },
                        });
                    }

                    $scope.checked = function(data) {
                        console.error("选择回调：", data);
                        $timeout(function() {
                            $scope.aurl.ctype = data;
                            $scope.path = $scope.xdata[data] || "/pages/index/index";
                            console.error("checked:", data, $scope.path);
                        });
                    }

                    $scope.xdata = {
                        "item": '/pages/goods/detail/index?item_id=',
                        "cat": '/pages/cat/index?cat_id=',
                        "page": '/pages/page/index?page_id=',
                        "video": "",
                        "phone": "",
                        "kanjia": "/pages/kanjia/rule/index?bargain_rule_id=",
                        "cover": "",
                        "previewImg": "",
                        // "coupon": "/pages/coupon/activity/index?card_id=",
                        "huodong": "/pages/huodong/index/index?huodong_id=",
                        "hongbao": "/pages/redenvelopes/recording/index?hongbao_id=",
                        "cainum": "/pages/luckDraw/index?draw_id=",
                        "index1": "/pages/index1/index",
                        "index2": "/pages/index2/index",
                        "index3": "/pages/index3/index",
                        "index4": "/pages/index4/index",
                        "index5": "/pages/index5/index",
                        "index6": "/pages/index6/index",
                        "index7": "/pages/index7/index",
                        "index8": "/pages/index8/index",
                        "index9": "/pages/index9/index",
                        "index10": "/pages/index10/index",
                        "yuyue": "/subpages/appointment/list/index",
                        "yue": "/pages/account/detail/index",
                        "daili": "/pages/distribution/add/index",
                        "pagelist": '/pages/activity/list/index',
                        "mianfu": 'mianfu',
                        "kecheng": "/pages/classes/list/index",
                        "expression": "/pages/expression/index",
                        "clockOnLeadGift": "/pages/gift/daka/index",
                        "topicTest": "/subpages/xingGeCeShi/diaoChaInfo/index",
                        "activitylist": "/pages/huodong/activitylist/index",
                        "wxXcxJump": "",
                        "wxmaterial": "/pages/weblink/index",
                        "flop": "/pages/gift/flop/index",
                        "caitu": "/pages/gift/zhaobutong/game/index",
                        "coupons_mjs": "/pages/coupon/detail/index",
                        "newtopic": "subpages/newtest/diaochainfo/index",
                        "newdaka": "subpages/newtest/daka/index",
                        "choicestore": "pages/choicestore/index",
                        "choicetab": "pages/tab/index",
                    };

                    //回调方法
                    $scope.itemdata = function(items, type, name, stype, subkey) {
                        console.error("!11111111111111111111111", $scope.aurl.ctype, items);
                        // console.error("stype", items, type, name, stype, subkey);
                        if ($scope.path && $scope.path.indexOf($scope.xdata[$scope.aurl.ctype]) != -1) {
                            $scope.path = $scope.xdata[$scope.aurl.ctype]
                        }

                        if (!$scope.path && $scope.aurl.ctype) {
                            $scope.path = $scope.xdata[$scope.aurl.ctype]
                        }

                        $timeout(function() {
                            var cid = "";

                            switch ($scope.aurl.ctype) {
                                case 'item':
                                    $scope.path += items.item_id;
                                    cid = items.item_id;
                                    break;

                                case 'cat':
                                    $scope.path += items;
                                    cid = items;
                                    break;

                                case 'page':
                                    $scope.path += items.activity_page_id;
                                    cid = items.activity_page_id;
                                    break;

                                case 'video':
                                    if (stype == "click_video") {
                                        $scope.path = items;
                                    } else if (stype == "click_cover") {
                                        $scope.cover = items;
                                    }
                                    console.error("video", items);
                                    break;

                                case 'phone':
                                    $scope.path = $scope.aurl.phone;
                                    break;

                                case 'kanjia':
                                    $scope.path += items;
                                    cid = items;
                                    break;

                                case 'cover':
                                    $scope.cover = items;
                                    $scope.path = cpath
                                    break;

                                case 'previewImg':
                                    $scope.path = items;
                                    break;

                                case 'kaquan':
                                    $scope.path += items;
                                    cid = items;
                                    break;

                                case 'huodong':
                                    $scope.path += items;
                                    break;

                                case 'hongbao':
                                    $scope.path += items;
                                    cid = items;
                                    break;

                                case 'cainum':
                                    $scope.path += items;
                                    cid = items;
                                    break;

                                case 'expression':
                                    $scope.path += "?item_id=" + items.item_id + "&confession_activity_id=" + items.confession_activity_id
                                    cid = items;
                                    break;
                                case 'wxXcxJump':
                                    $scope.path = $scope.aurl.appid;
                                    break;
                                case 'wxmaterial':
                                    // console.error("wxmaterial",items);
                                    var data = {
                                        title: items.title,
                                        url: items.url,
                                    }
                                    $scope.path += "?link=" + items.url + "&title=" + items.title;
                                    cid = angular.toJson(data);
                                    break;
                                case 'coupons_mjs':
                                    $scope.path += "?mjs_activity_id=" + items;
                                    cid = items;
                                    console.error("优惠劵");
                                    break;
                                case 'choicetab':
                                    $scope.path += "?label_id=" + items;
                                    cid = items;
                                    break;
                                case 'map':
                                    cid = items;
                                    $scope.cover = name;
                                    localtion = items.localtion;
                                    address = items.address;
                                    break;
                                case 'qiehuanstoreid':
                                    $scope.store = items;
                                    cid = items.id;
                                    break;
                            }
                            cpath = $scope.path;
                            $scope.cid = cid;
                        });
                    }

                    $scope.setdata = function() {
                        if ($scope.aurl.ctype == 'phone') {
                            $scope.path = $scope.aurl.phone;
                        } else if ($scope.aurl.ctype == 'wxXcxJump') {
                            $scope.path = $scope.aurl.appid;
                        } else if ($scope.aurl.ctype == 'map') {
                            $scope.cid = localtion;
                            $scope.cover = address;
                        }
                        var result = {
                            path: $scope.path,
                            ctype: $scope.aurl.ctype,
                            cid: $scope.cid,
                            cover: $scope.cover || ""
                        };
                        console.error("回调结果：", result);
                        $timeout(function() {
                            if ($scope.setback) {
                                showDialog.hide();
                                $scope.setback(result);
                                return true;
                            }
                        })
                    }
                    var marker = null;
                    var geocoder = null;
                    var geocoderStatus = 0;
                    var localtion = null;
                    var address = null;
                    $scope.initmap = function() {
                        console.error("111111");
                        mapObj = new qq.maps.Map(document.getElementById('qqMap'));
                        mapObj.panTo(new qq.maps.LatLng(39.916527, 116.397128));
                        mapObj.zoomTo(12);
                        qq.maps.event.addListener(
                            mapObj,
                            'click',
                            function(event) {
                                var center = new qq.maps.LatLng(event.latLng.getLat(), event.latLng.getLng());
                                geocoderStatus = 1;
                                if (!marker) {
                                    marker = new qq.maps.Marker({
                                        position: center,
                                        map: mapObj
                                    });
                                } else {
                                    marker.setMap(null);
                                    marker = new qq.maps.Marker({
                                        position: center,
                                        map: mapObj
                                    });
                                }
                                geocoder.getAddress(center);
                                // $scope.itemdata(center, null, null, 'click_map', null);
                                // alert('您点击的位置为:[' + event.latLng.getLng() +
                                //     ',' + event.latLng.getLat() + ']');
                            }
                        );
                        geocoder = new qq.maps.Geocoder({
                            complete: function(result) {
                                if (geocoderStatus == 2) {
                                    mapObj.setCenter(result.detail.location);
                                } else if (geocoderStatus == 1) {
                                    localtion = result.detail.location;
                                    address = result.detail.address;
                                }
                                // marker = new qq.maps.Marker({
                                //     map: mapObj,
                                //     position: result.detail.location
                                // });
                            }
                        });
                    }
                    $scope.searchName = function(name) {
                        geocoderStatus = 2;
                        geocoder.getLocation(name);
                        // console.error("mapName", $scope.searchLocationName);
                    }
                }],
                link: function(scope, element, attrs, ueditor) {}
            }
        });
})();